package com.lebang.controller.newapi;

import com.lebang.aop.Sessionid;
import com.lebang.common.Require;
import com.lebang.common.Util;
import com.lebang.entity.LbbAppVersion;
import com.lebang.entity.RequestHeaders;
import com.lebang.entity.member.LbbMember;
import com.lebang.entity.sys.AppMain;
import com.lebang.model.enums.Booleans;
import com.lebang.model.enums.Mode;
import com.lebang.model.enums.buss.RewardType;
import com.lebang.model.enums.buss.TradeCategory;
import com.lebang.service.AppMainService;
import com.lebang.service.buss.AppVersionService;
import com.lebang.service.buss.MemberService;
import com.lebang.service.buss.WalletService;
import com.lebang.service.sys.AppLookCategoryService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.lebang.controller.api.member.MemberApi.MEMBER_DATE;

/**
 * @Author: hwq
 * @Date: 2019/7/23 9:43
 * @Version 1.0
 */
@RestController
@RequestMapping("/new/api")
public class NewAppApi {
    @Autowired
    private AppMainService appMainService;

    @Autowired
    private AppVersionService appVersionService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private WalletService walletService;
    @Autowired
    private AppLookCategoryService appLookCategoryService;


    @PostMapping("/main")
    public JsonData main(HttpServletRequest request, Integer type) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        List<AppMain> list = appMainService.getListAppMain(requestHeaders, type);
        list=review(list,requestHeaders);
        return Callback.success("success", "查询成功", list);
    }

    @PostMapping("/version")
    public JsonData version(HttpServletRequest request) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        Map data = new HashMap(1);
        String version = requestHeaders.getVersion();
        String appid = requestHeaders.getAppid();
        String appStore = requestHeaders.getAppStore();
        String ipAddr = Util.getIpAddress(request);
        // 控制版本
        LbbAppVersion v = appVersionService.getByInnerCodeAndappStoreAndAppId(null, appStore, appid);
        if (v == null || "43.255.230.141".equals(ipAddr)) {
            // 屏蔽官网检测ip
            data.put("version", Integer.parseInt(version));
            data.put("appMode", "Debug");
            HashMap<String, Object> app = new HashMap<String, Object>(1);
            app.put("forceUpdate", Booleans.False.name());
            app.put("tipUser", Booleans.False.name());
            app.put("title", "");
            app.put("subtitle", "");
            app.put("version", "");
            app.put("content", "");
            app.put("href", "");
            app.put("sameVersionUpdate", Booleans.False.name());
            data.put("app", app);
        } else {
            LbbAppVersion v2 = appVersionService.getByInnerCodeAndappStoreAndAppId(Integer.parseInt(version), appStore, appid);
            Mode mode = Mode.Debug;
            if (v2 != null) {
                mode = v2.getAppMode();
            }
            data.put("version", v.getInnerCode());
            data.put("appMode", mode);
            HashMap<String, Object> app = new HashMap<String, Object>(1);
            app.put("forceUpdate", v.getForceUpdate());
            app.put("tipUser", v.getTipUser());
            app.put("title", v.getTitle());
            app.put("subtitle", v.getSubtitle());
            app.put("version", v.getVersion());
            app.put("content", v.getContent());
            app.put("href", v.getHref());
            app.put("sameVersionUpdate", v.getSameVersionUpdate());
            data.put("app", app);
        }

        return Callback.success("success", "查询成功", data);
    }


    /**
     * 用戶消息
     *
     * @param request
     * @return
     */
    @PostMapping("/membernotice")
    @Sessionid
    @Require
    public JsonData membernotice(HttpServletRequest request) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        String userid = requestHeaders.getUserid();
        LbbMember member = memberService.getMemberByMemberId(userid);
        return Callback.success("success", "查询成功", member.getNoticeCount());
    }

    @Require
    @PostMapping("/checkMember")
    @Sessionid
    public JsonData checkMember(HttpServletRequest request) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        String userid = requestHeaders.getUserid();
        if (memberService.selectMemberById(userid).getCreateTime().before(MEMBER_DATE)) {
            return Callback.fail("200", "", "");
        } else {
            return Callback.success("200", "", "");
        }
    }

    @Require
    @PostMapping("/task")
    @Sessionid
    public JsonData task(HttpServletRequest request, Integer type) {
        if(type==null){
            return Callback.fail("200", "", "");
        }
        RequestHeaders requestHeaders = new RequestHeaders(request);
        List<AppMain> list = appMainService.selectByType(requestHeaders,type);
        list=review(list,requestHeaders);
        return Callback.success("200", "", list);
    }

    @Require
    @PostMapping("/isrank")
    @Sessionid
    public JsonData isrank(HttpServletRequest request, Integer type) {
        if(type==null){
            return Callback.fail("200", "", "");
        }
        RequestHeaders requestHeaders = new RequestHeaders(request);
        List<AppMain> list = appMainService.selectByType(requestHeaders,type);
        list=review(list,requestHeaders);
        return Callback.success("200", "", list);
    }

    public List<AppMain> review(List<AppMain> list,RequestHeaders headers){
        Integer all = walletService.cash(headers.getUserid(), headers.getAppid(), TradeCategory.Income.name(), RewardType.Cash.name(), null, null);
//        List<String> stringList= Arrays.asList(dictService.selectByKeyName("filter_money_app_store").getKeyvalue().split("#"));
//
//        if (stringList.contains(headers.getAppStore())) {
//            list = list.stream().filter(s -> s.getLookMoney() <= all).collect(Collectors.toList());
//        }
        return appLookCategoryService.reviewAppMain(list,headers,all);
    }
}
